package com.ghp.common.utils;


import org.springframework.util.DigestUtils;

/**
 * @author ghp
 * @title 加密工具类
 * @description
 */
public class EncryptionUtils {

    private EncryptionUtils() {

    }

    /**
     * 加密次数
     */
    private static final int DEFAULT_COUNT = 3;

    /**
     * 盐
     */
    private static final String DEFAULT_SALT = "ghp";


    /**
     * md5带盐加密
     *
     * @param password 密码
     * @return
     */
    public static String getEncryptedPassword(String password) {
        return getEncryptedPassword(DEFAULT_SALT, password, DEFAULT_COUNT);
    }

    /**
     * md5带盐加密
     *
     * @param password 密码
     * @param count    次数
     * @return
     */
    public static String getEncryptedPassword(String password, int count) {
        return getEncryptedPassword(DEFAULT_SALT, password, count);
    }

    /**
     * md5带盐加密
     *
     * @param salt     盐
     * @param password 密码
     * @param count    次数
     * @return
     */
    public static String getEncryptedPassword(String salt, String password, int count) {
        String encryptedPassword = "";
        StringBuilder stringBuilder = new StringBuilder();
        while (count-- > 0) {
            // 获取ThreadLocal中的StringBuilder对象
            stringBuilder.append(password).append(salt);
            // md5加密
            encryptedPassword = DigestUtils.md5DigestAsHex(stringBuilder.toString().getBytes());
        }
        return encryptedPassword;
    }

    /**
     * 用于测试，无实际作用，可删除
     */
    public static void main(String[] args) {
        String password = "admin123";
        System.out.println(getEncryptedPassword(password));
    }
}
